package org.ruizhijie.i.impl;

import gnu.io.SerialPort;

import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ruizhijie.config.TopicQueue;
import org.ruizhijie.i.ISubcriber;

public class OutComSubcriberImpl implements ISubcriber{
	private Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	private TopicQueue tq;
	private String com;
	private SerialPort serialPort;
	
	public OutComSubcriberImpl(TopicQueue tq,String com,SerialPort serialPort){
		this.tq = tq;
		this.com = com;
		this.serialPort = serialPort;
		this.subcribe();
	}

	@Override
	public void subcribe() {
		tq.subcribe(this, com);
	}

	@Override
	public void unSubcribe() {
		tq.unSubcribe(this, com);
	}

	@Override
	public void onMessage(byte[] msg) {
		if (msg==null||msg.length<=0) return;
		try {
			serialPort.getOutputStream().write(msg);
			log.info("-----out-com-"+com+"-----"+msg.toString());
		} catch (IOException e) {
			e.printStackTrace();
		};
	}
}
